#region Copyright (c) 2003, newtelligence AG. All rights reserved.

/*
// Copyright (c) 2003, newtelligence AG. (http://www.newtelligence.com)
// Original BlogX Source Code: Copyright (c) 2003, Chris Anderson (http://simplegeek.com)
// All rights reserved.
//  
// Redistribution and use in source and binary forms, with or without modification, are permitted 
// provided that the following conditions are met: 
//  
// (1) Redistributions of source code must retain the above copyright notice, this list of 
// conditions and the following disclaimer. 
// (2) Redistributions in binary form must reproduce the above copyright notice, this list of 
// conditions and the following disclaimer in the documentation and/or other materials 
// provided with the distribution. 
// (3) Neither the name of the newtelligence AG nor the names of its contributors may be used 
// to endorse or promote products derived from this software without specific prior 
// written permission.
//      
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -------------------------------------------------------------------------
//
// Original BlogX source code (c) 2003 by Chris Anderson (http://simplegeek.com)
// 
// newtelligence is a registered trademark of newtelligence Aktiengesellschaft.
// 
// For portions of this software, the some additional copyright notices may apply 
// which can either be found in the license.txt file included in the source distribution
// or following this notice. 
//
*/

#endregion

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using newtelligence.DasBlog.Runtime;
using newtelligence.DasBlog.Web.Core.Config;

namespace newtelligence.DasBlog.Web.Core.Macros
{
    /// <summary>
    /// Like Macros, DayMacros always return controls.
    /// </summary>
    public class DayMacros : Macros
    {
        /// <summary>
        /// This value is expressed in display time (which depends on SiteConfig.AdjustDisplayTimeZone)
        /// </summary>
        protected DateTime day;

        /// <summary>
        /// Initialize a new instance of DayMacros.
        /// </summary>
        /// <param name="page">The page we are rendering</param>
        /// <param name="day">A day, expressed in display time, depending on SiteConfig.AdjustDisplayTimeZone</param>
        public DayMacros(SharedBasePage page, DateTime day)
            : base(page)
        {
            this.day = day;
        }

        /// <summary>
        /// Hyperlink to archived entries.
        /// Corresponds to the macro: &lt;%archiveLink%&gt;
        /// Works hand in hand with <see cref="DayMacros.LongDate"/>
        /// </summary>
        /// <remarks>The output is HTML hyperlinked HTML image.</remarks>
        public virtual Control ArchiveLink
        {
            get
            {
                HyperLink hl = new HyperLink();
                hl.CssClass = CoreCssStyles.Links.Archive;
                Image img = new Image();
                img.ImageUrl =
                    new Uri(new Uri(SiteUtilities.GetBaseUrl(requestPage.SiteConfig)),
                            requestPage.GetThemedImageUrl("dayLink")).ToString();
                img.CssClass = CoreCssStyles.Links.ArchiveImage;
                hl.Controls.Add(img);
                hl.NavigateUrl = SiteUtilities.GetDayViewUrl(day);

                return hl;
            }
        }

        /// <summary>
        /// Placeholder for rendering weblog entries.
        /// Corresponds to the macro: &lt;%items%&gt;
        /// </summary>
        /// <remarks>The output is HTML.</remarks>
        public virtual Control Items
        {
            get
            {
                PlaceHolder itemPlaceHolder = new PlaceHolder();
                EntryCollection entries = requestPage.WeblogEntries;

                foreach (Entry entry in entries)
                {
                    if ((!requestPage.SiteConfig.AdjustDisplayTimeZone && day.Year == entry.CreatedUtc.Year &&
                         day.Month == entry.CreatedUtc.Month && day.Day == entry.CreatedUtc.Day) ||
                        (requestPage.SiteConfig.AdjustDisplayTimeZone &&
                         requestPage.SiteConfig.GetConfiguredTimeZone().ToLocalTime(entry.CreatedUtc).Date ==
                         new DateTime(day.Year, day.Month, day.Day).Date))
                    {
                        requestPage.ProcessItemTemplate(entry, itemPlaceHolder);
                    }
                }

                return itemPlaceHolder;
            }
        }

        /// <summary>
        /// Protected field <see cref="DayMacros.day"/> formatted as long date.
        /// Corresponds to the macro: &lt;%longDate%&gt;
        /// Works hand in hand with <see cref="DayMacros.ArchiveLink"/>
        /// </summary>
        /// <remarks>The output is plain text.</remarks>
        public virtual Control LongDate
        {
            get { return new LiteralControl(day.ToString("D")); }
        }

        /// <summary>
        /// Protected field <see cref="DayMacros.day"/> formatted as whatever the
        /// user passes in as the format string.
        /// Corresponds to the macro: &lt;%dayFormattedDate(...)%&gt;
        /// Works hand in hand with <see cref="DayMacros.ArchiveLink"/>
        /// </summary>
        /// <param name="format">Standard format string</param>
        /// <remarks>The output is plain text.</remarks>
        public virtual Control DayFormattedDate(string format)
        {
            return new LiteralControl(day.ToString(format));
        }
    }
}